package com.adms.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adms.domain.Site;
import com.adms.domain.SiteExample;
import com.adms.domain.SiteExample.Criteria;
import com.adms.domain.SiteGridUser;
import com.adms.domain.SiteGridUserMapper;
import com.adms.domain.SiteHandleUser;
import com.adms.domain.SiteHandleUserMapper;
import com.adms.domain.SiteLegalUser;
import com.adms.domain.SiteLegalUserMapper;
import com.adms.domain.SiteMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

@Transactional
@Service
public class SiteService {

	@Autowired
	private SiteMapper siteMapper;
	@Autowired
	private SiteGridUserMapper siteGridUserMapper;
	@Autowired
	private SiteHandleUserMapper siteHandleUserMapper;
	@Autowired
	private SiteLegalUserMapper siteLegalUserMapper;

	public Site getBySiteId(Integer siteId) {
		return siteMapper.selectByPrimaryKey(siteId);
	}

	public Site getBySiteIdWithCompleted(Integer siteId) {
		return siteMapper.selectByPrimaryKeyWithCompleted(siteId);
	}

	public PageInfo<Site> list(Site site, String timeField, Date start, Date end,String orderByClause, String key,
			Integer pageNum, Integer pageSize, Integer navigatePages) {
		
		SiteExample ex = constructExample(site, timeField, start, end, orderByClause,key);
		PageHelper.startPage(pageNum, pageSize);
		List<Site> sites = siteMapper.selectByExample(ex);
		return new PageInfo<>(sites, navigatePages);
	}
	
	public PageInfo<Map<String,Object>> listWithCompleted(Site site, String timeField, Date start, Date end, String orderByClause,
			String key, Integer pageNum, Integer pageSize, Integer navigatePages) {
		
		SiteExample ex = constructExample(site, timeField, start, end, orderByClause,key);
		PageHelper.startPage(pageNum, pageSize);
		List<Map<String,Object>> sites = siteMapper.selectByExampleWithCompleted(ex);
		return new PageInfo<>(sites, navigatePages);
	}
	
	private SiteExample constructExample(Site site, String timeField, Date start, Date end,
			String orderByClause,String key) {
		SiteExample ex = new SiteExample();
		Criteria or = ex.or();
		if (site != null) {
			Integer gridId = site.getGridId();
			Integer type = site.getType();
			if(gridId != null) {
				or.andGridIdEqualTo(gridId);
			}
			if(type != null) {
				or.andTypeEqualTo(type);
			}
		}

		if (StringUtil.isNotEmpty(timeField)) {
			if (timeField.equals("createTime")) {
				if (start != null) {
					if (end != null) {
						or.andCreateTimeBetween(start, end);
					} else {
						or.andCreateTimeGreaterThanOrEqualTo(start);
					}
				} else {
					if (end != null) {
						or.andCreateTimeLessThanOrEqualTo(end);
					}
				}
			}
			if (timeField.equals("updateTime")) {
				if (start != null) {
					if (end != null) {
						or.andUpdateTimeBetween(start, end);
					} else {
						or.andUpdateTimeGreaterThanOrEqualTo(start);
					}
				} else {
					if (end != null) {
						or.andUpdateTimeLessThanOrEqualTo(end);
					}
				}
			}
		}

		if (StringUtil.isNotEmpty(orderByClause)) {
			ex.setOrderByClause(orderByClause);
		} else {
			ex.setOrderByClause("site.site_id");
		}
		
		if(StringUtil.isNotEmpty(key)) {
			or.andNameLike("%"+key+"%");
		}

		return ex;
	}

	public boolean create(Site site,int[] gridUserIds,Integer defaultGridUserId,
			int[] handleUserIds,Integer defaultHandleUserId,
			int[] legalUserIds,Integer defaultLegalUserId) {
		siteMapper.insertSelective(site);
		//设置关联网格员信息
		Integer siteId = site.getSiteId();
		if(gridUserIds != null && gridUserIds.length > 0) {
			List<SiteGridUser> siteGridUsers = new ArrayList<>();
			SiteGridUser user;
			for(Integer gridUserId:gridUserIds) {
				user = new SiteGridUser();
				user.setSiteId(siteId);
				user.setGridUserId(gridUserId);
				user.setIsDefault(defaultGridUserId == null?false:gridUserId.equals(defaultGridUserId)?true:false);
				siteGridUsers.add(user);
			}
			siteGridUserMapper.insertBatch(siteGridUsers);
		}
		//设置关联交办人信息
		if(handleUserIds != null && handleUserIds.length > 0) {
			List<SiteHandleUser> siteHandleUsers = new ArrayList<>();
			SiteHandleUser user;
			for(Integer handleUserId:handleUserIds) {
				user = new SiteHandleUser();
				user.setSiteId(siteId);
				user.setHandleUserId(handleUserId);
				user.setIsDefault(defaultHandleUserId == null?false:handleUserId.equals(defaultHandleUserId)?true:false);
				siteHandleUsers.add(user);
			}
			siteHandleUserMapper.insertBatch(siteHandleUsers);
		}
		//设置关联负责人信息
		if(legalUserIds != null && legalUserIds.length > 0) {
			List<SiteLegalUser> siteLegalUsers = new ArrayList<>();
			SiteLegalUser user;
			for(Integer legalUserId:legalUserIds) {
				user = new SiteLegalUser();
				user.setSiteId(siteId);
				user.setLegalUserId(legalUserId);
				user.setIsDefault(defaultLegalUserId == null?false:legalUserId.equals(defaultLegalUserId)?true:false);
				siteLegalUsers.add(user);
			}
			siteLegalUserMapper.insertBatch(siteLegalUsers);
		}
		return true;
	}

	public boolean update(Site site,int[] gridUserIds,Integer defaultGridUserId,
			int[] handleUserIds,Integer defaultHandleUserId,
			int[] legalUserIds,Integer defaultLegalUserId) {
		Integer siteId = site.getSiteId();
		//用来判断是否有数据要更新
		Site site2 = new Site();
		site2.setSiteId(site.getSiteId());
		if(!site.equals(site2)) {
			siteMapper.updateByPrimaryKeySelective(site);
		}
		//更新关联网格员信息
		if(gridUserIds == null || gridUserIds.length == 0) {
			siteGridUserMapper.deleteBySiteId(siteId);
		}else {
			List<SiteGridUser> siteGridUsers = new ArrayList<>();
			SiteGridUser user;
			for(Integer gridUserId:gridUserIds) {
				user = new SiteGridUser();
				user.setGridUserId(gridUserId);
				user.setIsDefault(defaultGridUserId == null?false:gridUserId.equals(defaultGridUserId)?true:false);
				siteGridUsers.add(user);
			}
			siteGridUserMapper.updateBatch(siteId,siteGridUsers);
		}
		//更新关联交办人信息
		if(handleUserIds == null || handleUserIds.length == 0) {
			siteHandleUserMapper.deleteBySiteId(siteId);
		}else {
			List<SiteHandleUser> siteHandleUsers = new ArrayList<>();
			SiteHandleUser user;
			for(Integer handleUserId:handleUserIds) {
				user = new SiteHandleUser();
				user.setHandleUserId(handleUserId);
				user.setIsDefault(defaultHandleUserId == null?false:handleUserId.equals(defaultHandleUserId)?true:false);
				siteHandleUsers.add(user);
			}
			siteHandleUserMapper.updateBatch(site.getSiteId(),siteHandleUsers);
		}
		//更新关联负责人信息
		if(legalUserIds == null || legalUserIds.length == 0) {
			siteLegalUserMapper.deleteBySiteId(siteId);
		}else {
			List<SiteLegalUser> siteLegalUsers = new ArrayList<>();
			SiteLegalUser user;
			for(Integer legalUserId:legalUserIds) {
				user = new SiteLegalUser();
				user.setLegalUserId(legalUserId);
				user.setIsDefault(defaultLegalUserId == null?false:legalUserId.equals(defaultLegalUserId)?true:false);
				siteLegalUsers.add(user);
			}
			siteLegalUserMapper.updateBatch(site.getSiteId(),siteLegalUsers);
		}
		return true;
	}

	public boolean delete(Integer[] siteIds) {
		if(siteIds != null) {
			int length = siteIds.length;
			if(length == 1) {
				siteMapper.deleteByPrimaryKey(siteIds[0]);
			}
			if(length > 1) {
				siteMapper.deleteBatch(siteIds);
			}
		}
		return true;
	}

}
